package com.guomei.controller.curse;

import com.guomei.bean.curse.AddOrUpdateCoupon;
import com.guomei.bean.curse.Coupon;
import com.guomei.constans.CouponCode;
import com.guomei.service.curse.CouponService;
import com.guomei.utils.Result;
import com.guomei.utils.ValidationUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 优惠券控制器
 */
@RestController
@RequestMapping("/api1/coupon")
@Slf4j
public class CouponController {

    @Resource
    private CouponService couponService;
    /**
     * 根据条件查询优惠券
     */
    @PostMapping("/search")
    public Result<?> searchCoupons(@RequestBody Coupon coupon) {
        log.info("根据条件查询优惠券：" + coupon);
        try {
            if (coupon == null || coupon.getPageNum() == null || coupon.getPageSize() == null){
                return Result.fail("分页字段信息缺失，查询失败");
            }
            // 验证分页参数是否有效
            if (coupon.getPageNum() <= 0 || coupon.getPageSize() <= 0) {
                return Result.fail("分页参数无效");
            }
            return Result.success(couponService.getByCondition(coupon));
        } catch (Exception e) {
            log.info("查询优惠券失败", e);
            return Result.fail("查询优惠卷失败");
        }
    }

    /**
     * 创建新优惠券
     */
    @PostMapping("/createCoupon")
    public Result<?> createCoupon(@Valid @RequestBody AddOrUpdateCoupon addOrUpdateCoupon, BindingResult result) {
        log.info("创建/修改优惠券入参：{}", addOrUpdateCoupon);
        //实体类注解参数校验
        Result commonDataResponse = ValidationUtil.validInputParams(result);
        if (commonDataResponse != null) {
            return commonDataResponse;
        }
        try {
            Coupon coupon = new Coupon();
            coupon.setCouponName(addOrUpdateCoupon.getCouponName());
            coupon.setCourseId(addOrUpdateCoupon.getCourseId());
            coupon.setCouponType(addOrUpdateCoupon.getCouponType());
            coupon.setDiscountValue(addOrUpdateCoupon.getDiscountValue());
            coupon.setMinPurchase(addOrUpdateCoupon.getMinPurchase());
            coupon.setStartTime(addOrUpdateCoupon.getStartTime());
            coupon.setEndTime(addOrUpdateCoupon.getEndTime());
            coupon.setTotalCount(addOrUpdateCoupon.getTotalCount());
            coupon.setUsedCount(addOrUpdateCoupon.getUsedCount());
            coupon.setCreatedTime(addOrUpdateCoupon.getCreatedTime());
            coupon.setUpdatedTime(addOrUpdateCoupon.getUpdatedTime());
            coupon.setTeacherId(addOrUpdateCoupon.getTeacherId());
            if (coupon.getId() == null){
                log.info("创建优惠券入参：{}", coupon);
                coupon.setStatus((short) CouponCode.RELEASE.getCode());
                return couponService.save(coupon);
            } else {
                log.info("修改优惠券入参：{}", coupon);
                coupon.setId(addOrUpdateCoupon.getId());
                coupon.setStatus(addOrUpdateCoupon.getStatus());
                return couponService.update(coupon);
            }
        } catch (Exception e) {
            log.info("创建/修改优惠券处理失败", e);
            return Result.error("处理失败");
        }
    }

    /**
     * 刪除優惠卷
     */
    @PostMapping("/deleteCoupon")
    public Result<?> deleteCoupon(@RequestBody Coupon coupon) {
        log.info("删除优惠券入参：{}", coupon);
        try {
            if (coupon == null || coupon.getId() == null) {
                return Result.fail("请选择要删除的优惠券");
            }
            return couponService.removeById(coupon.getId());
        } catch (Exception e) {
            log.info("删除优惠券处理失败", e);
            return Result.error("处理失败");
        }
    }
}
